package com.core.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XmlUtil {

	/**
	 * // 从文件读取XML，输入文件名，返回XML文档
	 * 
	 * @param fileName
	 * @return
	 * @throws MalformedURLException
	 * @throws DocumentException
	 */
	public Document read(String fileName) throws MalformedURLException,
			DocumentException {
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File(fileName));
		return document;
	}

	/**
	 * 取得Root节点
	 * 
	 * @param doc
	 * @return
	 */
	public Element getRootElement(Document doc) {
		return doc.getRootElement();
	}

	public void bar(Document document) throws DocumentException {

		Element root = document.getRootElement();

		// iterate through child elements of root
		for (Iterator i = root.elementIterator(); i.hasNext();) {
			Element element = (Element) i.next();
			// do something
		}

		// iterate through child elements of root with element name "foo"
		for (Iterator i = root.elementIterator("foo"); i.hasNext();) {
			Element foo = (Element) i.next();
			// do something
		}

		// iterate through attributes of root
		for (Iterator i = root.attributeIterator(); i.hasNext();) {
			Attribute attribute = (Attribute) i.next();
			// do something
		}
	}

	public Document createDocument() {
		Document document = DocumentHelper.createDocument();
		Element root = document.addElement("root");

		Element author1 = root.addElement("author")
				.addAttribute("name", "James").addAttribute("location", "UK")
				.addText("James Strachan");

		Element author2 = root.addElement("author").addAttribute("name", "Bob")
				.addAttribute("location", "US").addText("Bob McWhirter");

		return document;
	}

	public void write(Document document) throws IOException {

		// lets write to a file
		XMLWriter writer = new XMLWriter(new FileWriter("output.xml"));
		writer.write(document);
		writer.close();

		// Pretty print the document to System.out
		OutputFormat format = OutputFormat.createPrettyPrint();
		writer = new XMLWriter(System.out, format);
		writer.write(document);

		// Compact format to System.out
		format = OutputFormat.createCompactFormat();
		writer = new XMLWriter(System.out, format);
		writer.write(document);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		XmlUtil test = new XmlUtil();
		// String dirName = "d:/test/test0/test1/my2.xml";
		// CreateFileUtils.createDir(dirName);

		// test.create(
		// "E:/eclipse_springside/workspace/banklive/WebContent/xml/01.xml",
		// null);
		// // test.load("my1.xml");
		// // test.update("my1.xml");
	}

	// Attribute Attribute定义了XML的属性
	// Branch Branch为能够包含子节点的节点如XML元素(Element)和文档(Docuemnts)定义了一个公共的行为，
	// CDATA CDATA 定义了XML CDATA 区域
	// CharacterData CharacterData是一个标识借口，标识基于字符的节点。如CDATA，Comment, Text.
	// Comment Comment 定义了XML注释的行为
	// Document 定义了XML文档
	// DocumentType DocumentType 定义XML DOCTYPE声明
	// Element Element定义XML 元素
	// ElementHandler ElementHandler定义了 Element 对象的处理器
	// ElementPath 被 ElementHandler 使用，用于取得当前正在处理的路径层次信息
	// Entity Entity定义 XML entity
	// Node Node为所有的dom4j中XML节点定义了多态行为
	// NodeFilter NodeFilter 定义了在dom4j节点中产生的一个滤镜或谓词的行为（predicate）
	// ProcessingInstruction ProcessingInstruction 定义 XML 处理指令.
	// Text Text 定义XML 文本节点.
	// Visitor Visitor 用于实现Visitor模式.
	// XPath XPath 在分析一个字符串后会提供一个XPath 表达式

}
